package com.novacloud.controller;

import com.novacloud.core.BaseController;
import com.novacloud.core.Result;
import com.novacloud.domain.TUser;
import com.novacloud.exception.auth.LoginException;
import com.novacloud.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author <a href="mailto:huzhanjun@novacloud.com">huzj</a>
 * @version 1.0
 * @date 2017/10/10 14:50
 */
@Api("登录模块")
@RestController
@RequestMapping("/api/login")
public class LoginController extends BaseController{

    @Autowired
    private UserService userService;

    @ApiOperation("用户用于登录的接口，输入用户名和密码")
    @PostMapping
    public Result login(@RequestBody TUser request) {
        TUser tUser = userService.getByUsername(request.getUsername());
        if (tUser.getPassword().equals(request.getPassword())) {
            return resultSuccess(tUser);
        } else {
            throw new LoginException(LoginException.AUTH_USERNAME_PASSWORD_NOT_CORRECT);
        }
    }
}
